home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
198_01
/
eval.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-01-21
|
27KB
|
1,083 lines
/* EVAL.C: Expresion evaluation functions for
MicroEMACS
written 1986 by Daniel Lawrence */
#include <stdio.h>
#include "estruct.h"
#include "edef.h"
#include "evar.h"
varinit() /* initialize the user variable list */
{
register int i;
for (i=0; i < MAXVARS; i++)
uv[i].u_name[0] = 0;
}
char *gtfun(fname) /* evaluate a function */
char *fname; /* name of function to evaluate */
{
register int fnum; /* index to function to eval */
register int arg; /* value of some arguments */
char arg1[NSTRING]; /* value of first argument */
char arg2[NSTRING]; /* value of second argument */
char arg3[NSTRING]; /* value of third argument */
static char result[2 * NSTRING]; /* string result */
#if ENVFUNC
char *getenv();
#endif
/* look the function up in the function table */
fname[3] = 0; /* only first 3 chars significant */
mklower(fname); /* and let it be upper or lower case */
for (fnum = 0; fnum < NFUNCS; fnum++)
if (strcmp(fname, funcs[fnum].f_name) == 0)
break;
/* return errorm on a bad reference */
if (fnum == NFUNCS)
return(errorm);
/* if needed, retrieve the first argument */
if (funcs[fnum].f_type >= MONAMIC) {
if (macarg(arg1) != TRUE)
return(errorm);
/* if needed, retrieve the second argument */
if (funcs[fnum].f_type >= DYNAMIC) {
if (macarg(arg2) != TRUE)
return(errorm);
/* if needed, retrieve the third argument */
if (funcs[fnum].f_type >= TRINAMIC)
if (macarg(arg3) != TRUE)
return(errorm);
}
}
/* and now evaluate it! */
switch (fnum) {
case UFADD: return(itoa(atoi(arg1) + atoi(arg2)));
case UFSUB: return(itoa(atoi(arg1) - atoi(arg2)));
case UFTIMES: return(itoa(atoi(arg1) * atoi(arg2)));
case UFDIV: return(itoa(atoi(arg1) / atoi(arg2)));
case UFMOD: return(itoa(atoi(arg1) % atoi(arg2)));
case UFNEG: return(itoa(-atoi(arg1)));
case UFCAT: bytecopy(result, arg1, NSTRING-1);
bytecopy(&result[strlen(result)], arg2,
NSTRING-1-strlen(result));
return(result);
case UFLEFT: return(bytecopy(result, arg1, atoi(arg2)));
case UFRIGHT: arg = atoi(arg2);
if (arg > strlen(arg1)) arg = strlen(arg1);
return(strcpy(result,
&arg1[strlen(arg1)-arg]));
case UFMID: arg = atoi(arg2);
if (arg > strlen(arg1)) arg = strlen(arg1);
return(bytecopy(result, &arg1[arg-1],
atoi(arg3)));
case UFNOT: return(ltos(stol(arg1) == FALSE));
case UFEQUAL: return(ltos(atoi(arg1) == atoi(arg2)));
case UFLESS: return(ltos(atoi(arg1) < atoi(arg2)));
case UFGREATER: return(ltos(atoi(arg1) > atoi(arg2)));
case UFSEQUAL: return(ltos(strcmp(arg1, arg2) == 0));
case UFSLESS: return(ltos(strcmp(arg1, arg2) < 0));
case UFSGREAT: return(ltos(strcmp(arg1, arg2) > 0));
case UFIND: return(bytecopy(result, fixnull(getval(arg1)),
NSTRING-1));
case UFAND: return(ltos(stol(arg1) && stol(arg2)));
case UFOR: return(ltos(stol(arg1) || stol(arg2)));
case UFLENGTH: return(itoa(strlen(arg1)));
case UFUPPER: return(mkupper(arg1));
case UFLOWER: return(mklower(arg1));
case UFTRUTH: return(ltos(atoi(arg1) == 42));
case UFASCII: return(itoa((int)arg1[0]));
case UFCHR: result[0] = atoi(arg1);
result[1] = 0;
return(result);
case UFGTCMD: cmdstr(getcmd(), result);
return(result);
case UFGTKEY: result[0] = tgetc();
result[1] = 0;
return(result);
case UFRND: return(itoa((ernd() % abs(atoi(arg1))) + 1));
case UFABS: return(itoa(abs(atoi(arg1))));
case UFSINDEX: return(itoa(sindex(arg1, arg2)));
case UFENV:
#if ENVFUNC
return(fixnull(getenv(arg1)));
#else
return("");
#endif
case UFBIND: return(transbind(arg1));
case UFBAND: return(int_asc(asc_int(arg1) & asc_int(arg2)));
case UFBOR: return(int_asc(asc_int(arg1) | asc_int(arg2)));
case UFBXOR: return(int_asc(asc_int(arg1) ^ asc_int(arg2)));
case UFBNOT: return(int_asc(~asc_int(arg1)));
}
exit(-11); /* never should get here */
}
char *gtusr(vname) /* look up a user var's value */
char *vname; /* name of user variable to fetch */
{
register int vnum; /* ordinal number of user var */
/* scan the list looking for the user var name */
for (vnum = 0; vnum < MAXVARS; vnum++)
if (strcmp(vname, uv[vnum].u_name) == 0)
break;
/* return errorm on a bad reference */
if (vnum == MAXVARS)
return(errorm);
return(uv[vnum].u_value);
}
#if DECEDT
extern int advset;
#endif
char *gtenv(vname)
char *vname; /* name of environment variable to retrieve */
{
register int vnum; /* ordinal number of var refrenced */
char *getkill();
/* scan the list, looking for the referenced name */
for (vnum = 0; vnum < NEVARS; vnum++)
if (strcmp(vname, envars[vnum]) == 0)
break;
/* return errorm on a bad reference */
if (vnum == NEVARS)
return(errorm);
/* otherwise, fetch the appropriate value */
switch (vnum) {
case EVFILLCOL: return(itoa(fillcol));
case EVPAGELEN: return(itoa(term.t_nrow + 1));
case EVCURCOL: return(itoa(getccol(FALSE)));
case EVCURLINE: return(itoa(getcline()));
case EVRAM: return(itoa((int)(envram / 1024l)));
case EVFLICKER: return(ltos(flickcode));
case EVCURWIDTH:return(itoa(term.t_nrow));
case EVCBUFNAME:return(curbp->b_bname);
case EVCBFLAGS: return(itoa(curbp->b_flag));
case EVCFNAME: return(curbp->b_fname);
case EVSRES: return(sres);
case EVDEBUG: return(ltos(macbug));
case EVSTATUS: return(ltos(cmdstatus));
case EVPALETTE: return(palstr);
case EVASAVE: return(itoa(gasave));
case EVACOUNT: return(itoa(gacount));
case EVLASTKEY: return(itoa(lastkey));
case EVCURCHAR:
return(curwp->w_dotp->l_used ==
curwp->w_doto ? itoa('\n') :
itoa(lgetc(curwp->w_dotp, curwp->w_doto)));
case EVDISCMD: return(ltos(discmd));
case EVVERSION: return(VERSION);
case EVPROGNAME:return(PROGNAME);
case EVSEED: return(itoa(seed));
case EVDISINP: return(ltos(disinp));
case EVWLINE: return(itoa(curwp->w_ntrows));
case EVCWLINE: return(itoa(getwpos()));
case EVTARGET: saveflag = lastflag;
return(itoa(curgoal));
case EVSEARCH: return(pat);
case EVREPLACE: return(rpat);
case EVMATCH: return((patmatch == NULL)? "": patmatch);
case EVKILL: return(getkill());
case EVCMODE: return(itoa(curbp->b_mode));
case EVGMODE: return(itoa(gmode));
case EVTPAUSE: return(itoa(term.t_pause));
case EVPENDING:
#if TYPEAH
return(ltos(typahead()));
#else
return(falsem);
#endif
case EVLWIDTH: return(itoa(llength(curwp->w_dotp)));
case EVLINE: return(getctext());
case EVHARDTAB: return(int_asc(tabsize));
case EVSOFTTAB: return(int_asc(stabsize));
case EVFCOL: return(itoa(curwp->w_fcol));
case EVHSCROLL: return(ltos(hscroll));
case EVHJUMP: return(int_asc(hjump));
case EVADVANCE:
#if DECEDT
return(itoa(advset));
#else
return(itoa(1));
#endif
case EVVT100KEYS: return(itoa(vt100keys));
}
exit(-12); /* again, we should never get here */
}
char *fixnull(s) /* Don't return NULL pointers! */
char *s;
{
if (s == NULL)
return("");
else
return(s);
}
char *getkill() /* return some of the contents of the kill buffer */
{
register int size; /* max number of chars to return */
static char value[NSTRING]; /* temp buffer for value */
if (kbufh == NULL)
/* no kill buffer....just a null string */
value[0] = 0;
else {
/* copy in the contents... */
if (kused < NSTRING)
size = kused;
else
size = NSTRING - 1;
bytecopy(value, kbufh->d_chunk, size);
}
/* and return the constructed value */
return(value);
}
int setvar(f, n) /* set a variable */
int f; /* default flag */
int n; /* numeric arg (can overide prompted value) */
{
register int status; /* status return */
VDESC vd; /* variable num/type */
char var[NVSIZE+1]; /* name of variable to fetch */
char value[NSTRING]; /* value to set variable to */
/* first get the variable to set.. */
if (clexec == FALSE) {
status = mlreply("Variable to set: ", &var[0], NVSIZE);
if (status != TRU